#--------------------------------------------------------------------------
#----------------------------------数据准备----------------------------------
#--------------------------------------------------------------------------
from mnist import input_data
mnist=input_data.read_data_sets("MNIST_data/",one_hot=True)
print('输入数据：',mnist.train.images)
print('输入数据的尺寸：',mnist.train.images.shape)
import pylab
im=mnist.train.images[0]  #第一张图片
im=im.reshape(-1,28)
pylab.imshow(im)
pylab.show()
print("测试集大小：",mnist.test.images.shape)
print("验证集大小：",mnist.validation.images.shape)
#--------------------------------------------------------------------------
#----------------------------------定义网络----------------------------------
#--------------------------------------------------------------------------
import tensorflow as tf
tf.reset_default_graph()   #清除默认图形堆栈并重置全局默认图形
#定义占位符
x=tf.placeholder(tf.float32,[None,784])   #图像28*28=784
y=tf.placeholder(tf.float32,[None,10])    #标签10类
#定义学习参数
w=tf.Variable(tf.random_normal([784,10])) #权值，初始化为正太随机值
b=tf.Variable(tf.zeros([10]))             #偏置，初始化为0
#定义输出
pred=tf.nn.softmax(tf.matmul(x,w)+b)      #相当于单层神经网络，激活函数为softmax
#损失函数
cost=tf.reduce_mean(-tf.reduce_sum(y*tf.log(pred),reduction_indices=1))  #reduction_indices指定计算维度
#优化函数
optimizer=tf.train.GradientDescentOptimizer(0.01).minimize(cost)
#定义训练参数
training_epochs=25   #训练次数
batch_size=100       #每次训练图像数量
display_step=1       #打印训练信息周期
#保存模型
saver=tf.train.Saver()
model_path="log/521model.ckpt"
#--------------------------------------------------------------------------
#----------------------------------开始训练----------------------------------
#--------------------------------------------------------------------------
with tf.Session() as sess :
    sess.run(tf.global_variables_initializer())   #初始化所有参数
    for epoch in range(training_epochs) :
        avg_cost=0.                               #平均损失
        total_batch=int(mnist.train.num_examples/batch_size)   #计算总的训练批次
        for i in range(total_batch) :
            batch_xs, batch_ys=mnist.train.next_batch(batch_size)  #抽取数据
            _, c=sess.run([optimizer,cost], feed_dict={x:batch_xs, y:batch_ys})  #运行
            avg_cost+=c/total_batch
        if (epoch+1) % display_step == 0 :
            print("Epoch:",'%04d'%(epoch+1),"cost=","{:.9f}".format(avg_cost))
    print("Finished!")
    #测试集测试准确度
    correct_prediction=tf.equal(tf.argmax(pred,1),tf.argmax(y,1))
    accuracy=tf.reduce_mean(tf.cast(correct_prediction,tf.float32))
    print("Accuracy:",accuracy.eval({x:mnist.test.images,y:mnist.test.labels}))
    #保存模型
    save_path=saver.save(sess,model_path)
    print("Model saved in file: %s" % save_path)
#--------------------------------------------------------------------------
#----------------------------------开始测试----------------------------------
#--------------------------------------------------------------------------
print("Starting 2nd session...")
with tf.Session() as sess :
    sess.run(tf.global_variables_initializer())
    #恢复模型及参数
    saver.restore(sess,model_path)

    #测试
    correct_prediction=tf.equal(tf.argmax(pred,1),tf.arg_max(y,1))
    #计算准确度
    accuracy=tf.reduce_mean(tf.cast(correct_prediction,tf.float32))
    print("Accuracy: ",accuracy.eval({x:mnist.test.images,y:mnist.test.labels}))
    #计算输出
    output=tf.argmax(pred,1)
    batch_xs,batch_yx=mnist.train.next_batch(2)
    outputval,predv=sess.run([output,pred],feed_dict={x:batch_xs})
    print(outputval,predv,batch_ys)
    #显示图片1
    im=batch_xs[0]
    im=im.reshape(-1,28)
    pylab.imshow(im)
    pylab.show()
    #显示图片2
    im=batch_xs[1]
    im=im.reshape(-1,28)
    pylab.imshow(im)
    pylab.show()